package com.nxn.dinner.model;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.nxn.core.model.Customer;
import com.nxn.core.model.IdEntity;
import com.nxn.core.model.User;

@Entity
@Table(name = "t_delivery_order")
public class DeliveryOrder extends IdEntity {

	private String orderNo;
	private int type;
	private double price;
	private int payMode;
	private Date createTime;
	private Customer customer;
	private String contactName;
	private String contactAddress;
	private String road;
	private String doorNumber;
	private String floor;
	private String telephone;
	private Time deliveryTime;
	private String remarks;
	private DeliveryMan deliveryMan;
	private int status = OrderStatus.WAITING.getStatus();
	private User editor;
	private Date editTime;
	private Set<OrderList> orderSet = new HashSet<OrderList>();
	private List<OrderList> orderList = new ArrayList<OrderList>();
	
	
	@Column(name = "orderNo")
	public String getOrderNo() {
		return orderNo;
	}
	@Column(name = "type")
	public int getType() {
		return type;
	}
	@Column(name = "price")
	public double getPrice() {
		return price;
	}
	@Column(name = "payMode")
	public int getPayMode() {
		return payMode;
	}
	@Column(name = "createTime")
	@Temporal(TemporalType.TIMESTAMP)
	public Date getCreateTime() {
		return createTime;
	}
	@ManyToOne(cascade={CascadeType.MERGE})
    @JoinColumn(name="customer_id")
	public Customer getCustomer() {
		return customer;
	}
	@Column(name = "contactName")
	public String getContactName() {
		return contactName;
	}
	@Column(name = "contactAddress")
	public String getContactAddress() {
		return contactAddress;
	}
	@Column(name = "road")
	public String getRoad() {
		return road;
	}
	@Column(name = "doorNumber")
	public String getDoorNumber() {
		return doorNumber;
	}
	@Column(name = "floor")
	public String getFloor() {
		return floor;
	}
	@Column(name = "telephone")
	public String getTelephone() {
		return telephone;
	}
	@Column(name = "deliveryTime")
	public Time getDeliveryTime() {
		return deliveryTime;
	}
	@Column(name = "remarks")
	public String getRemarks() {
		return remarks;
	}
	@ManyToOne(cascade={CascadeType.MERGE})
    @JoinColumn(name="deliveryMan_id")
	public DeliveryMan getDeliveryMan() {
		return deliveryMan;
	}
	@Column(name = "status")
	public int getStatus() {
		return status;
	}
	@ManyToOne(cascade={CascadeType.MERGE})
    @JoinColumn(name="editor_id")
	public User getEditor() {
		return editor;
	}
	@Column(name = "editTime")
	@Temporal(TemporalType.TIMESTAMP)
	public Date getEditTime() {
		return editTime;
	}
	@OneToMany(mappedBy = "deliveryOrder")
	@Cascade({org.hibernate.annotations.CascadeType.MERGE,org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
	@Fetch(FetchMode.SUBSELECT)
	public Set<OrderList> getOrderSet() {
		return orderSet;
	}
	@Transient
	public List<OrderList> getOrderList() {
		return orderList;
	}
	
	
	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}
	public void setType(int type) {
		this.type = type;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public void setPayMode(int payMode) {
		this.payMode = payMode;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	public void setContactName(String contactName) {
		this.contactName = contactName;
	}
	public void setContactAddress(String contactAddress) {
		this.contactAddress = contactAddress;
	}
	public void setRoad(String road) {
		this.road = road;
	}
	public void setDoorNumber(String doorNumber) {
		this.doorNumber = doorNumber;
	}
	public void setFloor(String floor) {
		this.floor = floor;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	public void setDeliveryTime(Time deliveryTime) {
		this.deliveryTime = deliveryTime;
	}
	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
	public void setDeliveryMan(DeliveryMan deliveryMan) {
		this.deliveryMan = deliveryMan;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	public void setEditor(User editor) {
		this.editor = editor;
	}
	public void setEditTime(Date editTime) {
		this.editTime = editTime;
	}
	public void setOrderSet(Set<OrderList> orderSet) {
		this.orderSet = orderSet;
	}
	public void setOrderList(List<OrderList> orderList) {
		this.orderList = orderList;
	}

	/** 
	 * <p>现金支付</p><p>网络支付</p>
	 */
	public enum PayMode {
		CASH(1, "现金支付"),
		ONLINE(2, "网络支付");
		
		private int type;
		private String label;
		public int getType() {
			return type;
		}
		public void setType(int type) {
			this.type = type;
		}
		public String getLabel() {
			return label;
		}
		public void setLabel(String label) {
			this.label = label;
		}
		private PayMode(int type, String label) {
			this.type = type;
			this.label = label;
		}
		
	}
	
	/** 
	 * <p>3 + 2 or 2 + 1 </p>
	 */
	public enum OrderType {
		TWOPLUSONE(1),
		THREEPLUSTWO(2);
		
		private int orderType;
		public int getOrderType() {
			return orderType;
		}
		public void setOrderType(int orderType) {
			this.orderType = orderType;
		}

		private OrderType(int orderType) {
			this.orderType = orderType;
		}
	}
	/** 
	 * <p>等待处理</p><p>正在配餐</p><p>已送餐</p><p>完成</p><p>取消订单</p><p></p>
	 */
	public enum OrderStatus {
		WAITING(1, "等待处理"),
		PROCESSING(2, "正在配餐"),
		DELIVERING(3, "已送餐"),
		FINISH(4, "完成"),
		CANCEL(5, "取消订单");
		
		private int status;
		private String label;

		public int getStatus() {
			return status;
		}
		public void setStatus(int status) {
			this.status = status;
		}
		public String getLabel() {
			return label;
		}
		public void setLabel(String label) {
			this.label = label;
		}
		private OrderStatus(int status, String label) {
			this.status = status;
			this.label = label;
		}
	}
	
	public enum OrderSearchType {
		ORDERNO(1),
		CONTACTNAME(2);
		
		private int searchType;
		public int getSearchType() {
			return searchType;
		}

		public void setSearchType(int searchType) {
			this.searchType = searchType;
		}

		private OrderSearchType(int searchType) {
			this.searchType = searchType;
		}
		
	}
	
}
